package com.zry.traced.presenter

import android.content.Intent
import android.util.Log
import android.view.View
import com.zry.traced.MainActivity
import com.zry.traced.R
import com.zry.traced.greendao.entity.PutStorageCode
import com.zry.traced.ui.activity.BatchStorageActivity
import com.zry.traced.ui.activity.SubmitPutStorageActivity
import com.zry.traced.utils.*

class BatchStoragePresenter(private val activity: BatchStorageActivity) {


    fun codeSelectHandlerClick(view: View) {
        when (view.id) {
            R.id.submitConfirmPutStorageBtn -> {
                when (activity.title) {
                    "批量入库", "号段入库" -> {
                        if (activity.longAndShortCodeList!!.size == 0) {
                            return ToastUtil.toast(activity, "入库码数量为0，请先添加入库码")
                        }

                        val putStorageCode = getPutStorageCode()
                        putStorageCode.operationType = activity.title
                        activity.putStorageCodeDao!!.insert(putStorageCode)
                        setNextActivity(activity.title, putStorageCode.timeID)
                    }
                    "批量出库", "号段出库" -> {
                        if (activity.longAndShortCodeList!!.size == 0) {
                            return ToastUtil.toast(activity, "出库码数量为0，请先添加出库码")
                        }
                        activity.outTreasuryCode!!.codes = getShortListStr()
                        val outTreasuryCode = activity.outTreasuryCode!!
                        outTreasuryCode.operationType = activity.title
                        activity.outTreasuryCodeDao!!.insert(outTreasuryCode)
                        setNextActivity(activity.title, outTreasuryCode.timeID)
                    }
                    "批量装箱", "号段装箱" -> {
                        if (activity.longAndShortCodeList!!.size == 0) {
                            return ToastUtil.toast(activity, "装箱码数量为0，请先添加装箱码")
                        }
                        if (activity.longAndShortCodeList!!.size % activity.packingOperationInfo!!.packProportion.toInt() != 0) {
                            return ToastUtil.toast(activity, "扫码的数量必须是装箱比例的整数倍")
                        }
                        activity.packingOperationInfo!!.submitCode = getShortListStr()
                        val packingOperationInfo = activity.packingOperationInfo!!
                        packingOperationInfo.operationType = activity.title
                        activity.packingOperationInfoDao!!.insert(packingOperationInfo)

                        setNextActivity(activity.title, packingOperationInfo.timeID)
                    }
                }
            }
        }
    }

    private fun setNextActivity(title: String, TimeID: String) {
        if (!NetWorkUtils().isNetworkAvailable(activity)) {
            intentActivity()
        } else {
            val intent = Intent(activity, SubmitPutStorageActivity::class.java)
            intent.putExtra("title", title)
            intent.putExtra("TimeID", TimeID)
            activity.startActivity(intent)
        }
    }


    private fun intentActivity() {
        ToastUtil.toast(activity, "你没有连接网络，数据已经保存到本地")
        val intent = Intent(activity, MainActivity::class.java)
        activity.startActivity(intent)
    }

    private fun getPutStorageCode(): PutStorageCode {
        val putStorageCode = PutStorageCode(
                DateTimeUtilsKt().getNowDateTime("yyyyMMddHHmmss"),
                activity.storeOrderBean?.StoreOrderID,
                activity.storeOrderBean?.StoreOrderCode,
                activity.storeOrderBean?.ProductName,
                activity.storeOrderBean?.NetUnitPackName,
                activity.storeOrderBean?.StoreType,
                getShortListStr(),
                "", "", activity.title
        )
        Log.e("TAG", putStorageCode.toString())
        return putStorageCode
    }


    /**
     * 获取短码列表
     */
    private fun getShortListStr(): String {
        return StringListUtil().getStringCode(activity.longAndShortCodeList!!)
    }
}